﻿using System;
using System.Text;
using Aspects.Custom;

namespace Aspects.Unit
{
    class UnitSerialNumber
    {
        public static string GetSerialNumber()
        {
            var sb = new StringBuilder(8);
            var returnValue = "Serial Not Found"; //default value
            var SerialCharacter = (byte)0x00;
            for (byte i = (byte)ECGetSerial.GetSerial7; i >= (byte)ECGetSerial.GetSerial0; i--)
            {
                SerialCharacter = ECPort.DoubleReadEC(i);
                if (checkByte(SerialCharacter))
                    sb.Append((char)SerialCharacter);
            }
            SerialCharacter = ECPort.DoubleReadEC((byte)ECGetSerial.GetSerial8);
            if (checkByte(SerialCharacter))
                sb.Append((char)SerialCharacter);

            if (!string.IsNullOrEmpty(sb.ToString()))
                returnValue = sb.ToString();

            return returnValue;
        }

        /// <summary>
        /// Used to write the serial number to the embedded controller
        /// </summary>
        /// <param name="serial">a string with maximum of 9 characters.</param>
        public static void SetSerialNumber(string serial)
        {
            //TODO not implemented in the GUI, so not implemented here.
            /*
             * char 9 = 0x2B
             * sleep(50)
             * char 8 = 0x18
             * sleep(50)
             * char 7 = 0x19
             * sleep(50)
             * char 6 = 0x1A
             * sleep(50)
             * char 5 = 0x1B
             * sleep(50)
             * char 4 = 0x1C
             * sleep(50)
             * char 3 = 0x1D
             * sleep(50)
             * char 2 = 0x1E
             * sleep(50)
             * char 1 = 0x1F
             */
        }

        /// <summary>
        /// Checks a byte to see if it is a number, lower letter or big letter.
        /// </summary>
        /// <param name="biting">byte to be checked</param>
        /// <returns>if it is a number or letter it returns it self otherwise it returns the number 0</returns>
        private static bool checkByte(byte biting)
        {
            if (biting >= 0x30 && biting <= 0x39)
            {
                return true;
            }
            else if (biting >= 0x41 && biting <= 0x5A)
            {
                return true;
            }
            else if (biting >= 0x61 && biting <= 0x7A)
            {
                return true;
            }
            else
                return false;
        }

        private enum ECGetSerial : byte
        {
            GetSerial0 = 0x10,
            GetSerial1 = 0x11,
            GetSerial2 = 0x12,
            GetSerial3 = 0x13,
            GetSerial4 = 0x14,
            GetSerial5 = 0x15,
            GetSerial6 = 0x16,
            GetSerial7 = 0x17,
            GetSerial8 = 0x2A,
        }

        private enum ECSetSerial : byte
        {
            SetSerial0 = 0x18,
            SetSerial1 = 0x19,
            SetSerial2 = 0x1A,
            SetSerial3 = 0x1B,
            SetSerial4 = 0x1C,
            SetSerial5 = 0x1D,
            SetSerial6 = 0x1E,
            SetSerial7 = 0x1F,
            SetSerial8 = 0x2B,
        }
    }
}
